package com.example.module_maintenance.viewmodel

import androidx.lifecycle.MutableLiveData
import com.bw.lib_common.coroutine.CoroutineHelper
import com.bw.lib_common.vm.BaseViewModel2
import com.example.module_maintenance.entity.VisitorEntity
import com.example.module_maintenance.repo.VisitorsRepo
import kotlinx.coroutines.launch

class MaintenanceViewModel : BaseViewModel2<VisitorsRepo>(){
    val getVisitorsSuccess by lazy { MutableLiveData<List<VisitorEntity>>() }


    fun getVisitors() {
        CoroutineHelper.mViewModelScope.launch ( CoroutineHelper.mDispatcher ){
            val result = mRepository.getVisitors()
            if (result.code==200){
                getVisitorsSuccess.postValue(result.data)
            }else{
                mErrorLiveData.postValue("获取数据异常")
            }
        }
    }

    val addVisitorsSuccess by lazy { MutableLiveData<VisitorEntity>() }
    fun addVisitors(body : VisitorEntity){
         CoroutineHelper.mViewModelScope.launch ( CoroutineHelper.mDispatcher ){
             val result = mRepository.addVisitors(body)
             if (result.code==200){
                 addVisitorsSuccess.postValue(result.data)
             }else{
                 mErrorLiveData.postValue("添加失败")
             }
         }
    }






    override fun createRepository(): VisitorsRepo = VisitorsRepo()

}
